<?php
session_start();
        include "include/top.php"
?>
<?php
var_dump( $_POST );
var_dump( $_REQUEST );
var_dump( $_SESSION );
$name = $_REQUEST['name'];
$password = $_REQUEST['password'];

#$name = "test1";
#$password = "test1";

if ((!isset($name)) || (!isset($password))) 
{
//Visitor needs to enter a name and password
//echo "name=$name";
//echo "pwd=$password";
	echo "<h1>Need to login first</h1> <br />";

} 
else 
{
// connect to mysql
	$mysql = mysqli_connect("test2.transpolink.com", "ospms", "ospms");
	if(!$mysql) {
		echo "Cannot connect to database.";
		exit;
	}
// select the appropriate database
	$selected = mysqli_select_db($mysql, "OSPMS");
	if(!$selected) {
		echo "Cannot select database.";
		header("Location: https://test2.transpolink.com/OSPMS/index.php");
		exit;
	}
	$query = "select count(*) from user where user_name = '" .$name. "' and password = '". $password. "'" ;

//	echo "sql=$query\n";
//	echo "<br>\n";
	$result = mysqli_query($mysql, $query);
	if(!$result) {
		echo "Cannot run query.";
		exit;
	}
	$row = mysqli_fetch_row($result);
	var_dump($row);
	$count = $row[0];
	if ($count = 0) {
		// name and password combination are not correct
		echo "<h1>Please Leave!</h1>
		<p>You are not authorized to use this resource.</p>";
		header("Location: /OSPMS/index.php");
		exit;	
	}
// name and password combination are correct

	$query = "select user_name,first_name,password,user_id,role_id,group_id from user where user_name = '" .$name. "' " ;

	$result = mysqli_query($mysql, $query);
	$row = mysqli_fetch_row($result);

	$user_name = $row[0];
	$first_name = $row[1];
	$password = $row[2];
	$user_id = $row[3];
	$role_id = $row[4];
	$group_id  = $row[5];

	echo "--- row --- ";
	var_dump($row);

	$_SESSION['user_name'] = $user_name;
	$_SESSION['first_name'] = $first_name;
	$_SESSION['password'] = $password;
	$_SESSION['user_id'] = $user_id;
	$_SESSION['role_id'] = $role_id;
	$_SESSION['group_id']  = $group_id;

$source_location = dirname(__FILE__ );
print_r($source_location);

	echo "--- row --- ";
	header("Location: /OSPMS/ShowProjects.php");
#	header("Location: ShowProjects.php");


}  // BIG if
?>
<?php
        include "include/bottom.php"
?>
